#pragma once

#include <iostream>
#include <chrono>
#include <thread>
#include <sys/time.h>
using namespace std;
using namespace std::chrono;


inline uint32_t timerMicros(void) {
  uint32_t time_t;
  struct timespec tv = {0, 0};

  clock_gettime(CLOCK_MONOTONIC, &tv);
  time_t = tv.tv_sec * 1000000 + tv.tv_nsec / 1000;
  return time_t;
}
inline uint64_t timer_gettimeofday(void) {
  uint64_t time_t;
  struct timeval tv0;
  gettimeofday(&tv0, NULL);

  time_t = tv0.tv_sec * 1000000 + tv0.tv_usec;
  return time_t;
}

inline long long getCurrentTime() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  long long ms = tv.tv_sec;
  return ms * 1000 + tv.tv_usec / 1000;
}



inline int get_time_now() {
  typedef duration<int, std::milli> days_type;
  return time_point_cast<days_type>(system_clock::now())
      .time_since_epoch()
      .count();
}






class Timer {
 public:
  Timer() : m_begin(std::chrono::high_resolution_clock::now()) {}

  ~Timer() { }

  void reset() { m_begin = std::chrono::high_resolution_clock::now(); }

  //默认输出毫秒
  int64_t elapsed() const {
    return std::chrono::duration_cast<std::chrono::milliseconds>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

  //微秒
  int64_t elapsed_micro() const {
    return std::chrono::duration_cast<std::chrono::microseconds>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

  //纳秒
  int64_t elapsed_nano() const {
    return std::chrono::duration_cast<std::chrono::nanoseconds>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

  //秒
  int64_t elapsed_seconds() const {
    return std::chrono::duration_cast<std::chrono::seconds>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

  //分
  int64_t elapsed_minutes() const {
    return std::chrono::duration_cast<std::chrono::minutes>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

  //时
  int64_t elapsed_hours() const {
    return std::chrono::duration_cast<std::chrono::hours>(
               std::chrono::high_resolution_clock::now() - m_begin)
        .count();
  }

 private:
  std::chrono::time_point<std::chrono::high_resolution_clock> m_begin;
};
